package com.yyg.boot.web;

import com.yyg.boot.entity.Book;
import com.yyg.boot.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/book")
public class ElasticController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 创建索引
     */
    @PostMapping("/create")
    public void createIndex() {
        elasticsearchTemplate.createIndex(Book.class);
    }

    @PostMapping("/save")
    public void Save() {
        Book book = new Book("100", "ElasticSearch第2种实现方式", "一一哥", "2020-04-13");
        log.warn(book.toString());
        bookService.save(book);
    }

    @GetMapping("/id/{id}")
    public Book getBookById(@PathVariable String id) {
        Optional<Book> opt = bookService.findById(id);
        Book book = opt.get();
        log.warn(book.toString());
        return book;
    }

}
